﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCE.UI.Blocks;
using Baosight.ZNH.ZMCE.UI.PopWindows;

namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCE20 : EFViewCmes
    {
        private NoteBlock block = new NoteBlock();
        
        public ZMCE20()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            initData.AddBlock(block.getNoteType("cmes.ce.noteType", true));
            //initData.AddBlock(block.getNoteStatus("cmes.ce.noteStatus", true));
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击反馈按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                //根据2012-2-24会议讨论暂停的工单可以反馈，并且可以多次反馈。直到工单完成。
//                if ("暂停".Equals(_list[0]["fdStatus"]))
//                {
//                    this.OpenEFWindowAlert("只能反馈执行状态的工单!", true);
//                    return;
//                }
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);            

            base.OnUpdateButtonClickPopup("反馈", eiInfo, "工单反馈-反馈", "update", null, sender, e);
        }

        ///// <summary>
        ///// 反馈前的判断
        ///// </summary>
        ///// <param name="_eIRows"></param>
        ///// <returns></returns>
        //public override bool BeforeMethod(List<EIRow> _eIRows)
        //{
        //    bool Flag = true;
        //    if ("完成".Equals(_eIRows[0]["fdStatus"].ToString()) || "暂停".Equals(_eIRows[0]["fdStatus"].ToString()))
        //    {
        //        base.OpenEFWindowAlert("不能对状态为完成、暂停的工单进行反馈！", true);
        //        Flag = false;
        //    }
        //    return Flag;
        //}

        ///// <summary>
        ///// 查看明细
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        //{
        //    HyperlinkButton link = sender as HyperlinkButton;
        //    EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
        //    EIInfo eiInfo = new EIInfo();
        //    EIBlock eIBlock = new EIBlock("result");
        //    eIBlock.SetRow(0, row == null ? new EIRow() : row);
        //    eiInfo.AddBlock(eIBlock);

        //    base.OnHyperlinkButtonClickPopup(eiInfo, "工单反馈明细-查看", "detail", null, sender, e);
        //}

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            NoteFeedback note = new NoteFeedback(_dialogType);//构造一个新的修改页面
            note.EIDataContext = new Platform.Core.UI.EIDataContext();
            
            _eIInfo.AddBlock(block.getCondition("cmes.ce.condition", false));
            _eIInfo.AddBlock(block.getDegree("cmes.ce.degree", false));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));
            _eIInfo.AddBlock(new NoteBlock().getIntactType("cmes.ce.intactType"));
            note.EIDataContext.Update(_eIInfo);

            note.fdExecutionPersonId.Value = this.EIDataContext.GetAttribute("fdPersonId");
            (note.fdExecutionPersonId.Content as CmesCustomView).Display = this.EIDataContext.GetAttribute("fdPerson");
            note.fdExecutionPerson.Value = this.EIDataContext.GetAttribute("fdPerson");
            note.fdTrueEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            if ("大修".Equals(_eIInfo.Get("result-0-fdType")) || "维修".Equals(_eIInfo.Get("result-0-fdType")))
            {
                note.more.IsEnabled=false;
            }
            note.fdCondition.Value = " ";
            (note.fdExecutionPersonId.Content as CmesCustomView).eViewInitParams = "bindingService=ZM03.query,columns=empCode|empName,queryConditions=empCode|empName,initParam=orgCode:" + this.EIDataContext.GetAttribute("fdDepartmentId");

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = note;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }


    }
}
